/**
 * 
 */
package xj.util.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class OptionPanel extends JPanel {

  public static final int BUTTON_APPLY = 0;

  public static final int BUTTON_OK = 1;

  public static final int BUTTON_CANCEL = 2;

  public static final int APPLY_OK_CANCEL = 0;

  public static final int OK_CANCEL = 1;

  public OptionPanel(String name) {
    this(name, APPLY_OK_CANCEL, null, null);
  }

  public OptionPanel(String name, int buttonStyle) {
    this(name, buttonStyle, null, null);
  }

  public OptionPanel(String name, 
      	       int buttonStyle, 
      	       Icon[] buttonIcons,
      	       String[] buttonText) {
    this.name = name;
    this.buttonStyle = buttonStyle;
    this.buttonIcons = buttonIcons;
    this.buttonText = buttonText;

    setLayout(new BorderLayout());
    mainPanel = new JPanel();
    add(mainPanel, BorderLayout.CENTER);

    //mainPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), name));
    mainPanel.setBorder(BorderFactory.createEtchedBorder());
    mainPanel.add(new JLabel(name), BorderLayout.NORTH);

    JPanel actionPanel = new JPanel();
    add(actionPanel, BorderLayout.SOUTH);
    actionPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 11, 11));
    if (buttonStyle == APPLY_OK_CANCEL) {
      String applyText = "Apply";
      if (buttonText != null && buttonText.length >= 3
          && buttonText[BUTTON_APPLY] != null) {
        applyText = buttonText[BUTTON_APPLY];
      }
      JButton applyButton = new JButton(applyText);
      if (buttonIcons != null && buttonIcons.length >= 3
          && buttonIcons[BUTTON_APPLY] != null) {
        applyButton.setIcon(buttonIcons[BUTTON_APPLY]);
      }
      actionPanel.add(applyButton);
      applyButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          applyOption();
        }
      });
    }
    String okText = "OK";
    if (buttonText != null && buttonText.length >= 3
        && buttonText[BUTTON_OK] != null) {
      okText = buttonText[BUTTON_OK];
    }
    JButton okButton = new JButton(okText);
    if (buttonIcons != null && buttonIcons.length >= 3
        && buttonIcons[BUTTON_OK] != null) {
      okButton.setIcon(buttonIcons[BUTTON_OK]);
    }
    actionPanel.add(okButton);
    okButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        applyOption();
        if (dialog != null) {
          dialog.setVisible(false);
        }
        dialogClosed();
      }
    });
    String cancelText = "Cancel";
    if (buttonText != null && buttonText.length >= 3
        && buttonText[BUTTON_CANCEL] != null) {
      cancelText = buttonText[BUTTON_CANCEL];
    }
    JButton cancelButton = new JButton(cancelText);
    if (buttonIcons != null && buttonIcons.length >= 3
        && buttonIcons[BUTTON_CANCEL] != null) {
      cancelButton.setIcon(buttonIcons[BUTTON_CANCEL]);
    }
    actionPanel.add(cancelButton);
    cancelButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (dialog != null) {
          dialog.setVisible(false);
        }
        dialogClosed();
      }
    });
  }

  public void addRow(Component left, Component center) {
    mainPanel.add(left, DialogLayout.LEFT);
    mainPanel.add(center, DialogLayout.CENTER);
  }

  public void addRow(Component left, Component center, Component right) {
    mainPanel.add(left, DialogLayout.LEFT);
    mainPanel.add(center, DialogLayout.CENTER);
    mainPanel.add(right, DialogLayout.RIGHT);
  }

  @Override
  public String getName() {
    return name;
  }

  public void setDialog(JDialog dialog) {
    this.dialog = dialog;
  }

  public void setListener(ChangeListener listener) {
    this.listener = listener;
  }

  public void setOption() {
  }

  public void applyOption() {
    if (listener != null) {
      listener.stateChanged(new ChangeEvent(this));
    }
  }

  public void dialogClosed() {
  }

  public int getButtonStyle() {
    return buttonStyle;
  }

  @Override
  public Dimension getPreferredSize() {
    return dim;
  }

  @Override
  public Dimension getMinimumSize() {
    return minDim;
  }

  @Override
  public void setPreferredSize(Dimension dim) {
    this.dim = dim;
  }

  @Override
  public void setMinimumSize(Dimension minDim) {
    this.minDim = minDim;
  }

  protected String name;

  protected ChangeListener listener;

  protected JDialog dialog;

  protected JPanel mainPanel;

  protected int buttonStyle = APPLY_OK_CANCEL;

  protected Icon[] buttonIcons = null;

  protected String[] buttonText = null;

  protected Dimension dim = defaultDim;

  protected Dimension minDim = defaultMinDim;

  private static Dimension defaultDim = new Dimension(300, 200);

  private static Dimension defaultMinDim = new Dimension(200, 100);

}
